package socket.Client;

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.net.Socket;
import java.net.UnknownHostException;

public class Client {

    public static final String ADRESSEIP = "157.169.103.157";

    public static void main(String[] args) {

        Socket mySocket = null; // le socket client
        PrintStream os = null; // output stream
        BufferedReader is = null; // input stream

        try {
            mySocket = new Socket(ADRESSEIP, 2345);
            os = new PrintStream(mySocket.getOutputStream());
            is = new BufferedReader(new InputStreamReader(mySocket
                    .getInputStream()));
        } catch (UnknownHostException e) {
            System.err.println("Don't know about host: " + ADRESSEIP);
        } catch (IOException e) {
            System.err.println("Couldn't get I/O for the connection to"
                    + ADRESSEIP);
        }

        if (mySocket != null && os != null && is != null) {
            try {
                boolean ok = true;
                String responseLine;
                while (ok) {
                    while ((responseLine = is.readLine()) != null) {
                        System.out.println("Server: " + responseLine);
                        if (responseLine.equals("QUIT")){
                            ok = false;
                            break;
                        }
                        if (responseLine.equals("STOP"))
                            break;
                    }
                    if (!ok)
                        break;

                    BufferedReader in = new BufferedReader(
                            new InputStreamReader(System.in));
                  String s = in.readLine();  
                  os.println(s);
                }
                os.close();
                is.close();
                mySocket.close();
            } catch (UnknownHostException e) {
                System.err.println("Trying to connect to unknown host: " + e);
            } catch (IOException e) {
                System.err.println("IOException: " + e);
            }
        }

    }

}